package com.lhz.project.system.controller.auth;


import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.lhz.common.page.BaseQueryParam;
import com.lhz.common.page.ResponseObject;
import com.lhz.framework.aspect.log.annotation.SysLog;
import com.lhz.framework.security.annotation.CheckPermission;
import com.lhz.project.system.model.entity.auth.Role;
import com.lhz.project.system.model.param.auth.RoleParam;
import com.lhz.project.system.model.vo.auth.RoleDeptVo;
import com.lhz.project.system.model.vo.auth.RoleMenuVo;
import com.lhz.project.system.service.auth.RoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

/**
 * @author LiHuaZhi
 * @since 2020-06-20 13:42:57
 */
@Tag(name = "角色管理")
@RestController
@RequestMapping("role")
@ApiSort(15)
public class RoleController {

    @Resource
    private RoleService roleService;


    @Operation(summary = "根据Id查询", description = "根据Id查询")
    @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Role.class))))
    @ApiOperationSupport(order = 5)
    @SysLog(model = "角色管理", name = "根据Id查询")
    @GetMapping("/{id}")
    @CheckPermission(code = "system:role:query")
    public ResponseObject selectByPrimaryKey(@PathVariable("id") Integer id) {
        Object obj = roleService.selectByPrimaryKey(id);
        return ResponseObject.success(obj);
    }

    @Operation(summary = "查询列表", description = "查询列表")
    @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Role.class))))
    @ApiOperationSupport(order = 10)
    @SysLog(model = "角色管理", name = "查询列表")
    @GetMapping
    @CheckPermission(code = "system:role:query")
    public ResponseObject listRole(@ParameterObject BaseQueryParam param) {
        Object obj = roleService.listRole(param);
        return ResponseObject.success(obj);
    }

    @Operation(summary = "新增数据", description = "新增数据")
    @ApiOperationSupport(order = 15)
    @SysLog(model = "角色管理", name = "新增数据")
    @PostMapping
    @CheckPermission(code = "system:role:add")
    public ResponseObject insert(@RequestBody RoleParam param) {
        roleService.insert(param);
        return ResponseObject.successPost();
    }

    @Operation(summary = "更新数据", description = "更新数据")
    @ApiOperationSupport(order = 20)
    @SysLog(model = "角色管理", name = "更新数据")
    @PutMapping
    @CheckPermission(code = "system:role:update")
    public ResponseObject updateByPrimaryKey(@RequestBody RoleParam param) {
        roleService.updateByPrimaryKey(param);
        return ResponseObject.successUpdate();
    }

    @Operation(summary = "删除数据", description = "删除数据")
    @ApiOperationSupport(order = 25)
    @Parameters({
            @Parameter(name = "id", description = "数据id", in = ParameterIn.PATH, schema = @Schema(type = "int"), required = true)
    })
    @SysLog(model = "角色管理", name = "删除数据")
    @DeleteMapping("delete/{id}")
    @CheckPermission(code = "system:role:delete")
    public ResponseObject deletePhyByPrimaryKey(@PathVariable("id") Integer id) {
        roleService.deletePhyByPrimaryKey(id);
        return ResponseObject.successDel();
    }


    @Operation(summary = "查询菜单信息", description = "查询菜单信息")
    @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = RoleMenuVo.class))))
    @ApiOperationSupport(order = 30)
    @Parameters({
            @Parameter(name = "roleId", description = "角色Id", in = ParameterIn.PATH, schema = @Schema(type = "int"), required = true)
    })
    @SysLog(model = "角色管理", name = "查询菜单信息")
    @GetMapping("/menu/{roleId}")
    @CheckPermission(code = {"system:role:update", "system:role:add"})//更新新增时获取
    public ResponseObject getRoleMenuById(@PathVariable("roleId") Integer roleId) {
        Object obj = roleService.getRoleMenuById(roleId);
        return ResponseObject.success(obj);
    }

    @Operation(summary = "查询数据权限", description = "查询数据权限")
    @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = RoleDeptVo.class))))
    @ApiOperationSupport(order = 35)
    @Parameters({
            @Parameter(name = "roleId", description = "角色Id", in = ParameterIn.PATH, schema = @Schema(type = "int"), required = true)
    })
    @SysLog(model = "角色管理", name = "查询数据权限")
    @GetMapping("role/{roleId}")
    @CheckPermission(code = {"system:role:update", "system:role:add"})//更新新增时获取
    public ResponseObject getRoleById(@PathVariable("roleId") Integer roleId) {
        Object obj = roleService.getRoleDeptById(roleId);
        return ResponseObject.success(obj);
    }
}